<template>
<!-- 登录组件 -->
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">考勤管理系统</div>
            <!-- model：表单数据对象，rules：表单验证规则，ref：给元素或子组件注册引用信息 -->
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <!-- 用户名表单 -->
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="员工编号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <!-- 密码表单 -->
                <el-form-item prop="password">
                    <el-input
                        placeholder="密码"
                        v-model="param.password"
                        @keypress.enter.native="submitForm()"
                        show-password
                    ><!--  .native - 主要是给自定义的组件添加原生事件 -->
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
// import bus from '../../utils/bus'
export default {
    data: function() {
        return {
            param: {
                username: '',
                password: '',
            },
            rules: {
                username: [{ required: true, message: '请输入员工编号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        submitForm() {
            var that = this;
            // 获取ref注册的引用信息
            this.$refs.login.validate(valid => {
                if (valid) {
                    var bcrypt = require('bcryptjs');    //引入bcryptjs库
                    var salt = bcrypt.genSaltSync(10);    //定义密码加密的计算强度,默认10
                    var hash = bcrypt.hashSync(this.param.password, salt);
                    var postData = {
                        username: this.param.username,
                        password: hash
                    };
                    that.$store.commit("username", postData.username);
                    that.$store.commit("password", postData.password);
                    // bus.$emit('username', that.param.username);
                    // bus.$emit('password-encoded', hash);
                    //fetch('http://localhost:9090/approval');
                    //const csrfToken = this.getCookie('XSRF-TOKEN');
                    fetch('http://localhost:9095/login', {
                        method: 'POST',
                        //credentials: 'include',
                        body: JSON.stringify(postData),
                        headers: new Headers({
                            'Content-Type': 'application/json',
                            'username': that.param.username 
                        }),
                        mode: 'cors'
                    }).then(res => {console.log(res); return res.json();})
                    .then(json => {console.log('loginStatus', json.loginStatus);
                                    if (json.loginStatus == 'success') {
                                        sessionStorage.setItem('token', json.Token);
                                        sessionStorage.setItem('username', that.param.username);
                                        that.$store.commit("token", json.Token);
                                        fetch('http://localhost:9095/AttendanceSystem/employee/id'+"?empID="+that.param.username, {
                                            method: 'GET',
                                            headers: new Headers({
                                                'Content-Type': 'application/json',
                                                'username': that.param.username,
                                                'Token': that.$store.state.token 
                                            })
                                        }).then(res => {return res.json()})
                                        .then(json => {
                                            that.$store.commit("emp_name", json.empName);
                                            that.$store.commit("department", json.deptName);
                                            that.$store.commit("department_id", json.deptID);
                                            that.$store.commit("auth", json.auth);
                                            that.$router.push('/info');
                                        });
                                    }})
                    .catch(err => {console.log('请求错误', err);});
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!');
                    return false;
                }
            });
        },
    },
    created() {
        let leave_cnt = localStorage.getItem("leave_cnt");
        if (leave_cnt == null) {
            localStorage.setItem("leave_cnt", 0)
            console.log()
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
</style>
